/// 每一道题都需要自己把名字打一遍，太麻烦了
/// 写一个工具
extern crate clipboard;

use clipboard::ClipboardProvider;
use clipboard::ClipboardContext;
use std::fs::{File, OpenOptions};
use std::io::Write;

/// 最佳到哪个文件夹，修改这里就可以了
const MODULE_STR: &str = "tree";

/// 从剪贴板中获取题目的名字
fn get_name_from_clipboard() -> String {
    let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
    ctx.get_contents().ok().unwrap()
}

/// 转化方法名
pub fn convert_name() -> String {
    let name = get_name_from_clipboard();
    let vec: Vec<&str> = name.split(|c| c == ' ').collect::<Vec<&str>>();
    let mut result = String::from("");
    vec.into_iter().for_each(|x| {
        let mut word: String = x.chars().enumerate().map(|(_, c)| {
            if c.is_uppercase() {
                c.to_lowercase().to_string()
            } else {
                c.to_string()
            }
        }).collect();
        word += "_";
        result.push_str(&word)
    });
    result.pop();
    result
}

// 创建对应的文件
pub fn create_file(name: &str) {
    let name = "./src/".to_string() + MODULE_STR + "/" + name + ".rs";
    println!("name {}", name);
    File::create(name).expect("file  create fail");
}

// 修改mod文件
// 最佳到哪个文件
pub fn modify_mod_file(name: &str) {
    let string = "mod ".to_string() + name + ";";
    let file_name = "./src/".to_string() + MODULE_STR + "/sliding_windows";
    println!("file_name {}", file_name);
    println!("string {}", string);
    let mut file = OpenOptions::new().append(true).open(file_name).expect("open file fail");
    file.write_all(string.as_bytes()).expect("write fail");
}

pub fn leetcode_question_name_util() {
    let name = convert_name();
    println!("{} ", name);
    create_file(&name);
    modify_mod_file(&name);
}
